<?php declare(strict_types=1);


namespace App\Services\Quota;

use Illuminate\Support\Facades\Log;

class EasypayQuotaService implements GetQuotaInterface
{

    protected function __construct()
    {
    }

    const ORG = "ORG000000001897";

    protected static $base;

    protected static $instance;

    public static function getInstance()
    {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        if (is_null(self::$base)) {
            self::$base = (new \App\Api\Controllers\EasyPay\MerchantController());
        }
        return self::$instance;
    }


    public function getQuota(string $mchNo, array $config): array
    {
        if (!self::$base instanceof \App\Api\Controllers\EasyPay\MerchantController) {
            return [false, null];
        }
        $actRet = json_decode(self::$base->act_query(['mchtCode' => $mchNo, 'reqId' => $config["orgId"] ?? self::ORG], $config["key"]), true);
        Log::channel("quota")->info("easypayGetQuota_" . $mchNo, [
            "reqData" => ['mchtCode' => $mchNo, 'reqId' => $config["orgId"] ?? self::ORG],
            "resData" => $actRet
        ]);
        //                $actRet = json_decode('{"data":{"marketingDetailList":[{"accumulationType":"1","activityId":1756,"beginDate":"2025-02-16T00:00:00","billingCycle":"0","businessAmount":300.00,"businessModel":"1","businessSceneCode":"ALL;UnionPromoCNP,UnionPromoCP","cardIssuerCode":"ALL","cardType":"XXXX","countCycle":"0","createBy":"UMP","createDatetime":"2025-02-15T18:35:13","discountType":"2","discountValue":0.30,"endAmount":999999.00,"endDate":"2025-03-15T23:59:59","instalNum":"ALL","marketingQuotaUsed":31.74,"mchtCode":"531000020795644","payAbilityCode":"WeChat,AliPay;UnionPay","payMethodCode":"ALL;ALL","quotaId":6664085,"registrationId":13362250,"registrationLevel":4,"registrationStatus":"1","startAmount":0.01,"updateDatetime":"2025-02-15T18:35:15"},{"accumulationType":"1","activityId":741,"beginDate":"2025-01-04T18:00:00","billingCycle":"0","businessAmount":600.00,"businessModel":"1","businessSceneCode":"ALL;UnionPromoCNP,UnionPromoCP","cardIssuerCode":"ALL","cardType":"XXXX","countCycle":"0","createBy":"UMP","createDatetime":"2025-01-04T17:15:00","discountType":"2","discountValue":0.30,"endAmount":999999.00,"endDate":"2025-02-15T23:59:59","instalNum":"ALL","marketingQuotaUsed":600.00,"mchtCode":"531000020795644","payAbilityCode":"WeChat,AliPay;UnionPay","payMethodCode":"ALL;ALL","quotaId":6161397,"registrationId":12914367,"registrationLevel":4,"registrationStatus":"1","startAmount":0.01,"updateDatetime":"2025-01-04T17:15:00"}],"retCode":"000000","retMsg":"处理成功"},"success":true,"sysRetCode":"000000","sysRetMsg":"SUCCESS"}', true);
        if (!isset($actRet["data"])) {
            $actRet["data"] = $actRet['rspBody'];
        }
        $resData = [
            'company_code' => $config["company"],
            'mch_id' => $mchNo,
            'start_date' => "",
            'end_date' => "",
            'quota' => 0
        ];
        $isGet = false;
        if (isset($actRet['data']['marketingDetailList']) && !empty($actRet['data']['marketingDetailList'])) {
            foreach ($actRet['data']['marketingDetailList'] as $vv) {
                if ($vv['registrationStatus'] == 1) {
                    $quota = $vv['businessAmount'] - $vv['marketingQuotaUsed'];
                    if (date('m') != date('m', strtotime($vv['beginDate']))) {
                        continue;
                    }
                    if ($quota == 0) {
                        continue;
                    }
                    $isGet = true;
                    $startDate = date('Ymd', strtotime($vv['beginDate']));
                    $endDate = date('Ymd', strtotime($vv['endDate']));
                    $resData = [
                        'company_code' => $config["company"],
                        'mch_id' => $mchNo,
                        'start_date' => $startDate,
                        'end_date' => $endDate,
                        'quota' => $quota
                    ];
                    break;
                }
            }
        }
        return [$isGet, $resData];
    }


}